package com.highcom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.highcom.pojo.User;
import com.highcom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/mainframe")
    public String userLogin(User user){
        System.out.println(user.getUser_name()+","+user.getUser_password());
        User user1 = userService.login(user);
        if(user1 != null){
            return "redirect:mainframe.jsp";
        }else{
            return "login.jsp";
        }
    }

    @RequestMapping("/yonghuxinxi")
    public ModelAndView findAllRoles(@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo) {
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<User> roleList = userService.findUsers();
        //把要分页的集合数据添加到PageInfo中
        PageInfo<User> pageInfo = new PageInfo<>(roleList);
        m.addObject("userInfo", pageInfo);
        m.setViewName("/files/yonghuxinxi.jsp");
        return m;
    }

    @RequestMapping("/delUserById")
    public String delUserById(int id) {
        System.out.println(id);
        userService.delUserById(id);
        return "redirect:/yonghuxinxi";
    }

    @RequestMapping("/delUsersByids")
    public String delUsersByIds(int[] ids){
        userService.delUsersByIds(ids);
        return "redirect:/yonghuxinxi";
    }

    @RequestMapping("/findUserById")
    public ModelAndView findUserById(int id) {
        ModelAndView m = new ModelAndView();
        User user = userService.findUserById(id);
        m.addObject("userInfo", user);
        m.setViewName("/files/yonghumodify.jsp");
        return m;
    }

    @RequestMapping("/modifyUser")
    public String modifyUser(User user) {
        System.out.println(user.getId());
        userService.modifyUser(user);
        return "redirect:/yonghuxinxi";
    }

    @RequestMapping("/addUser")
    public String addUser(User user) {
        userService.addUser(user);
        return "redirect:/yonghuxinxi";
    }

}
